package com.hjj.springboot.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hjj.springboot.config.LogType;
import com.hjj.springboot.config.SystemLogs;
import com.hjj.springboot.constant.SystemConstant;
import com.hjj.springboot.entity.Admin;
import com.hjj.springboot.entity.R;
import com.hjj.springboot.service.IAdminService;
import com.hjj.springboot.util.DateUtil;
import com.hjj.springboot.util.JwtUtils;
import com.hjj.springboot.util.StringUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Value("${avatarUrlImagesFilePath}")
    private String avatarUrlImagesFilePath;

    @SystemLogs(operation = "管理员登录",type = LogType.LOGIN)
    @PostMapping("/adminLogin")
    public R adminLogin(@RequestBody Admin admin) {
        if(admin==null){
            return R.error();
        }
        if(StringUtil.isEmpty(admin.getUserName())||StringUtil.isEmpty(admin.getPassword())){
            return R.error("用户名或密码不能为空！");
        }
        Admin resultAdmin = adminService.getOne(new QueryWrapper<Admin>().eq("userName", admin.getUserName()));
        if(resultAdmin==null){
            return R.error("用户名不存在");
        }
        if (!resultAdmin.getPassword().trim().equals(admin.getPassword())) {
            return R.error("用户名或密码错误！");
        }
        String token = JwtUtils.createJWT(resultAdmin.getId().toString(),resultAdmin.getUserName(), SystemConstant.JWT_TTL);
        resultAdmin.setLastLoginDate(new Date());
        adminService.updateById(resultAdmin);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("token", token);
        resultMap.put("userInfo", resultAdmin);
        return R.ok(resultMap);
    }

    @SystemLogs(operation = "管理员注册",type = LogType.REGISTER)
    @PostMapping("/adminRegister")
    public  R adminRegister(@RequestBody Admin admin){
        if(admin==null){
            return R.error();
        }
        if(StringUtil.isEmpty(admin.getUserName())||StringUtil.isEmpty(admin.getPassword())){
            return R.error("用户名或密码不能为空！");
        }
        Admin resultAdmin = adminService.getOne(new QueryWrapper<Admin>().eq("userName", admin.getUserName()));
        if(resultAdmin!=null){
            return R.error("用户名已存在");
        }
        admin.setUserName(admin.getUserName());
        admin.setPassword(admin.getPassword());
        admin.setRegisterDate(new Date());
        adminService.save(admin);
        return R.ok();
    }

    @SystemLogs(operation = "管理员个人密码",type = LogType.UPDATE)
    @PostMapping("/modifyPassword")
    public R modifyPassword(@RequestBody Admin admin){
        if(StringUtil.isEmpty(admin.getUserName())||StringUtil.isEmpty(admin.getPassword())){
            return R.error("用户名或密码不能为空");
        }
        adminService.update(new UpdateWrapper<Admin>().set("password", admin.getNewPassword()).eq("userName", admin.getUserName()));
        return R.ok();
    }

    @SystemLogs(operation = "管理员个人头像",type = LogType.UPLODE)
    @RequestMapping("/uplodeAvatarUrl")
    public Map<String,Object> uplodeAvatarUrl(MultipartFile file) throws Exception {
        Map<String,Object> reultMap=new HashMap<>();
        if(!file.isEmpty()){
            String originalFilename= file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName= DateUtil.getCurrentDateStr()+suffix;
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File(avatarUrlImagesFilePath+newFileName));
            reultMap.put("code",0);
            reultMap.put("msg","上传成功");
            Map<String,Object> dataMap=new HashMap<>();
            dataMap.put("title",newFileName);
            dataMap.put("src","img/avatarUrl/"+newFileName);
            reultMap.put("data",dataMap);
        }
        return reultMap;
    }

    @SystemLogs(operation = "管理员个人信息",type = LogType.UPDATE)
    @RequestMapping("/adminSave")
    public R save(@RequestBody Admin admin){
        if(StringUtil.isEmpty(admin.getTelNumber())){
            return R.error("电话号码不能为空！");
        }
        adminService.updateById(admin);
        return R.ok();
    }
}
